package com.snailvr.tv.ui.view;

import android.app.AlertDialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snailvr.tv.Definition;
import com.snailvr.tv.ui.seekbar.DiscreteSeekBar;
import com.snailvr.tv.util.SPUtils;


/**
 *
 * 对话框集合类
 * Created by snail on 16/4/19.
 */
public class FovDialog {

    public static class Builder {
        private Context mContext;
        private DiscreteSeekBar.OnProgressChangeListener mProgressChangeListener;
        private View.OnClickListener mSubmitListener;
        private View.OnClickListener mCancelListener;


        public Builder(Context context, DiscreteSeekBar.OnProgressChangeListener progressChangeListener) {
            mContext = context;
            mProgressChangeListener = progressChangeListener;
        }


        public Builder addSubmitListener(View.OnClickListener submitListener) {
            mSubmitListener = submitListener;
            return this;
        }

        public Builder addCancelListener(View.OnClickListener cancelListener) {
            mCancelListener = cancelListener;
            return this;
        }

        public AlertDialog show() {
            View view = LayoutInflater.from(mContext).inflate(R.layout.select_fov_dialog, null);
            final AlertDialog dialog = new AlertDialog.Builder(mContext,R.style.SingleChoiceDialog).setView(view).create();
            final DiscreteSeekBar discreteSeekBar=(DiscreteSeekBar)view.findViewById(R.id.discrete);

            if (mProgressChangeListener!=null)
                discreteSeekBar.setOnProgressChangeListener(mProgressChangeListener);

            discreteSeekBar.setValue((int) SPUtils.get(mContext, Definition.KEY_FOV, Definition.FOV_DEFAULT));
            DisplayMetrics dm = mContext.getResources().getDisplayMetrics();

            int discreteSeekBarWidth=dm.widthPixels-mContext.getResources().getDimensionPixelSize(R.dimen.dimen_330)
                    -mContext.getResources().getDimensionPixelSize(R.dimen.padding_16);

            int bestAngleWidth=discreteSeekBarWidth/7;

            TextView minAngle = (TextView) view.findViewById(R.id.best_min_angle);

            TextView maxAngle = (TextView) view.findViewById(R.id.best_max_angle);

            RelativeLayout.LayoutParams minAngleParams=(RelativeLayout.LayoutParams) minAngle.getLayoutParams();
            RelativeLayout.LayoutParams maxAngleParams=(RelativeLayout.LayoutParams) maxAngle.getLayoutParams();
            if(minAngleParams!=null&&maxAngleParams!=null) {
                int marginLeft=discreteSeekBarWidth * 2 / 7+mContext.getResources().getDimensionPixelSize(R.dimen.padding_16);
                minAngleParams.leftMargin=marginLeft;
                maxAngleParams.leftMargin=marginLeft+bestAngleWidth/2+mContext.getResources().getDimensionPixelSize(R.dimen.padding_12);
            }

            final TextView submit = (TextView) view.findViewById(R.id.btn_submit);

            TextView cancel = (TextView) view.findViewById(R.id.btn_cancle);

            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setTag(discreteSeekBar.getValue());
                    SPUtils.put(mContext, Definition.KEY_FOV, discreteSeekBar.getValue());
                    dialog.dismiss();
                    mSubmitListener.onClick(v);
                }
            });

            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    if(mCancelListener!=null)
                        mCancelListener.onClick(view);
                }
            });



            dialog.show();
            dialog.getWindow().setLayout(dip2px(mContext,330), dip2px(mContext,236));

            dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

            return  dialog;
        }




    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context , float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }



}
